package com.stu.dao.impl;

import com.stu.dao.ManageDao;
import com.stu.model.User;
import com.stu.util.DaoUtile;
import com.stu.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManageDaoImpl implements ManageDao {
    //获取不为管理员的所有账户
    @Override
    public List<User> getAllUser() throws SQLException {
        String sql="select * from user where userIdentify!=2";
        return DaoUtile.getAllUser(sql);
    }
//模糊查询所有信息除管理员的信息
    @Override
    public List<User> getSearch(String searchText) throws SQLException {
        String sql="select * from user where userIdentify!=2 and (userAccount='"+searchText+"'or userName like '%"+searchText+"%' or userIdCard='"+searchText+"')";
        return DaoUtile.getAllUser(sql);
    }
     //判断生成的账户是否可用
    @Override
    public int isExistPersonAccount(String userAccount) throws SQLException {
        String sql="select count(*) num from user where userAccount='"+userAccount+"'";
        return DaoUtile.isExist(sql);
    }

    @Override
    public void addUser(User user) throws SQLException {
       /* String sql="insert into user values ('"+user.getUserAccount()+"','"+user.getUserName()+"','"+user.getUserSex()+
                "','"+user.getUserBirthday()+"','"+user.getUserIdCard()+"','"+user.getUserPassword()+"','"+user.getUserIdentify()+
                "','"+user.getUserOtherName()+"')";*/

        String sql="insert into user values ('"+user.getUserAccount()+"','"+user.getUserName()+"','"+user.getUserSex()+
                "','"+user.getUserPassword()+"','"+user.getUserIdentify()+"','"+user.getUserOtherName()+"','"+user.getUserBirthday()+
                "','"+user.getUserIdCard()+"')";
      /*  System.out.println(sql);*/
        DaoUtile.notQuery(sql);

    }
//修改密码
    @Override
    public void sureUpPassword(String userAccount, String userPassword) throws SQLException {
        String sql="update user set userPassword='" + userPassword + "' where userAccount='" + userAccount + "'";
        System.out.println(sql);
        DaoUtile.notQuery(sql);
    }
     //删除三张表中某人的所有信息
    @Override
    public void delThisPerson(String delAccount, String userIdentify) throws SQLException {
        if(Integer.parseInt(userIdentify)==0){
            //删除学生
            String sql1="delete from userteacher where studentAccount='"+delAccount+"'";
            DaoUtile.notQuery(sql1);
            String sql2="delete from studenttask where studentAccount='"+delAccount+"'";
            String sql3="delete from user where userAccount='"+delAccount+"'";
            DaoUtile.notQuery(sql2);
            DaoUtile.notQuery(sql3);

        }else{
            //删除老师
            String sql3="delete from studenttask where taskAccount in(select taskAccount from task where teacherAccount='"+delAccount+"')";
            String sql4="delete from task where teacherAccount='"+delAccount+"'";
            String sql5="delete from userteacher where teacherAccount='"+delAccount+"'";
            DaoUtile.notQuery(sql3);
            DaoUtile.notQuery(sql4);
            DaoUtile.notQuery(sql5);
        }
        String sql6="delete from user where userAccount='"+delAccount+"'";
        DaoUtile.notQuery(sql6);
    }


}
